package com.example.thornchg.yc_plane_game;

import android.view.MotionEvent;

import com.example.thornchg.yc_plane_game.enemy.Enemy;

import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.sound.SoundEngine;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;
import org.cocos2d.types.CGSize;


public class GameLayer extends CCLayer {
    private EnemyCreator enemyCreator;
    private CGSize winSize;
    private CGPoint cgPoint_touchPoint;
    private Hero hero;
    private int score;
    private CCLabel ccLabel;
    private GameState gameState;
    private CCLabel ccLabel_gameOver;

    public enum GameState {
        GAMEOVER, GAME
    }

    public GameLayer() {
        SoundEngine soundEngine = SoundEngine.sharedEngine();
        soundEngine.preloadSound(CCDirector.sharedDirector().getActivity(), R.raw.bgm);
        soundEngine.preloadEffect(CCDirector.theApp,R.raw.enemy1_dead);
        SoundEngine.sharedEngine().preloadEffect(CCDirector.theApp,R.raw.game_over);
        soundEngine.playSound(CCDirector.sharedDirector().getActivity(), R.raw.bgm, true);
        winSize = CCDirector.sharedDirector().getWinSize();
        gameState = GameState.GAME;
        // 创建地图
        CombatMap combatMap = new CombatMap(this);


        // 创建玩家战机
        hero = new Hero();
        addChild(hero);
        hero.shootBullet();
        enemyCreator = new EnemyCreator(this);
        enemyCreator.start();
        cgPoint_touchPoint = null;

        score = 0;
        //开启定时器 创建敌机
        ccLabel = CCLabel.makeLabel("分数:" + score, "Arial", 40);
        ccLabel.setColor(ccc3(255, 255, 255));
        ccLabel.setAnchorPoint(0, 1);
        ccLabel.setPosition(ccp(10, winSize.getHeight()));
        addChild(ccLabel);

        //开启触摸事件
        setIsTouchEnabled(true);
        scheduleUpdate();



    }

    private void reStart() {
        hero.resume();
        hero.shootBullet();
        enemyCreator.start();
        score=0;
        ccLabel.setString("分数:" + score);
        scheduleUpdate();
    }


    @Override
    public boolean ccTouchesBegan(MotionEvent event) {

        switch (gameState) {
            case GAME:
                if (cgPoint_touchPoint == null)
                    cgPoint_touchPoint = convertTouchToNodeSpace(event);
                break;
            case GAMEOVER:
                gameState=GameState.GAME;
                reStart();
                ccLabel_gameOver.setVisible(false);
                break;
        }

        return super.ccTouchesBegan(event);
    }

    @Override
    public boolean ccTouchesMoved(MotionEvent event) {
        switch (gameState) {
            case GAME:
                //触摸移动玩家战机
                if (hero.getVisible() && cgPoint_touchPoint != null) {
                    CGPoint cgPoint = convertTouchToNodeSpace(event);
                    hero.move(ccp(cgPoint.x - cgPoint_touchPoint.x, cgPoint.y - cgPoint_touchPoint.y));
                    cgPoint_touchPoint.set(cgPoint);
                }
                break;
        }
        return super.ccTouchesMoved(event);
    }

    @Override
    public boolean ccTouchesEnded(MotionEvent event) {
        cgPoint_touchPoint = null;
        return super.ccTouchesEnded(event);
    }

    public void update(float t) {
        boolean temp;
        CGRect cgRectE = new CGRect();
        CGRect cgRect = new CGRect();
        for (Enemy enemy : enemyCreator.getEnemys_1()) {
            if (enemy.getVisible()) {
                cgRectE.set(enemy.getPosition().x - enemy.getBoundingBox().size.getWidth() / 2,
                        enemy.getPosition().y,
                        enemy.getBoundingBox().size.getWidth(),
                        enemy.getBoundingBox().size.getHeight());
                temp = false;
                for (CCSprite bullet : hero.getBullets()) {
                    if (bullet.getVisible() && !enemy.isDead()) {
                        cgRect.set(
                                bullet.getPosition().x - bullet.getBoundingBox().size.getWidth() / 2,
                                bullet.getPosition().y,
                                bullet.getBoundingBox().size.getWidth(),
                                bullet.getBoundingBox().size.getHeight()
                        );
                        if (CGRect.intersects(cgRectE, cgRect)) {
                            enemy.beAttacked(hero);
                            bullet.stopAllActions();
                            bullet.setVisible(false);
                            temp = true;
                            if (enemy.isDead()){
                                score += enemy.getScore();
                                ccLabel.setString("分数:" + score);
                            }
                            break;
                        }
                    }
                }
                if (temp) continue;
                cgRect.set(
                        hero.getPosition().x - hero.getBoundingBox().size.getWidth() / 2,
                        hero.getPosition().y - hero.getBoundingBox().size.getHeight() / 2,
                        hero.getBoundingBox().size.getWidth(),
                        hero.getBoundingBox().size.getHeight()
                );
                if (CGRect.intersects(cgRectE, cgRect)) {
                    unscheduleUpdate();
                    hero.stopAllActions();
                    hero.showExplosion(this);
                    break;
                }
            }
        }
    }

    public void gameOver() {
        /*gameState = GameState.GAMEOVER;
        enemyCreator.stop();
        CCScheduler.sharedScheduler().unschedule("createHeroBullet", hero);

        if (ccLabel_gameOver == null) {
            ccLabel_gameOver = CCLabel.makeLabel("Game Over", "Arial", 100);
            ccLabel_gameOver.setColor(ccc3(0, 0, 0));
            ccLabel_gameOver.setPosition(winSize.getWidth() / 2, winSize.getHeight() / 2);
            addChild(ccLabel_gameOver);
        }
        ccLabel_gameOver.setVisible(true);*/
        CCScene ccScene=CCScene.node();
        ccScene.addChild(new GameOverLayer(score));
        CCDirector.sharedDirector().runWithScene(ccScene);

    }

}
